package com.yiren.manager.ctrl.pension;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.Favorites;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrIndiServiceCost;
import com.yiren.entity.YrIndiServiceCostHistory;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.pension.YrIndiServiceCostForm;
import com.yiren.manager.service.iface.pension.YrIndiServiceCostService;
import com.yiren.manager.utils.ObjectUtils;

/**
 * 个性服务费控制类
 * @author Administrator
 *
 */
@RequestMapping("indiServiceCost")
@Controller
public class YrIndiServiceCostController extends GenericController<YrIndiServiceCostForm, YrIndiServiceCost>{

	@Autowired
	private YrIndiServiceCostService yrIndiServiceCostService;
	
	// 保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrIndiServiceCostForm> save(String data)
			throws SwallowException, MyException, InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, IntrospectionException {
		HTTPResult<YrIndiServiceCostForm> result = new HTTPResult<YrIndiServiceCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrIndiServiceCostForm form = tool.mapper("indiServiceCost", YrIndiServiceCostForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if (null == form.getIndiServiceCostId() || form.getIndiServiceCostId().isEmpty()) {
			form.setIndiServiceCostId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());

		// 实体
		YrIndiServiceCost entity = ObjectUtils.objectToObject(form, YrIndiServiceCost.class);
		// 修改历史
		YrIndiServiceCostHistory history = ObjectUtils.objectToObject(entity, YrIndiServiceCostHistory.class);
		
		Favorites favorites = new Favorites();
		favorites.putFavorite(YrIndiServiceCost.class, entity);
		favorites.putFavorite(YrIndiServiceCostHistory.class, history);

		yrIndiServiceCostService.addByFavorites(favorites);

		return result;
	}
	
	/** 获取所有记录 */
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrIndiServiceCostForm> findAll() throws MyException, InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		HTTPResult<YrIndiServiceCostForm> result = new HTTPResult<YrIndiServiceCostForm>();
		List<YrIndiServiceCost> entityList = yrIndiServiceCostService.findAll();
		List<YrIndiServiceCostForm> formList = new ArrayList<YrIndiServiceCostForm>();
		for (YrIndiServiceCost serviceCost : entityList) {
			YrIndiServiceCostForm form = ObjectUtils.objectToObject(serviceCost, YrIndiServiceCostForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrIndiServiceCostForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrIndiServiceCostForm> result = new HTTPResult<YrIndiServiceCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			yrIndiServiceCostService.delete(id);
		}
		return result;
	}

	
	@Override
	public YrIndiServiceCostForm toForm(YrIndiServiceCost e) throws Exception {
		YrIndiServiceCostForm form = ObjectUtils.objectToObject(e, YrIndiServiceCostForm.class);
		return form;
	}

	@Override
	public YrIndiServiceCost toEntity(YrIndiServiceCostForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
